// pages/manageCardAll/manageCardAll.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    modal_title:'',
    modal_content:'',
    currentTab:-1,
    index:-1,
    page:1,
    couponList:[],//优惠券列表

  },
  // 优惠券liet
  couponList(id){

    let that = this;
    that.setData({page:1})
    app.post("/coupon/list","get",{page:that.data.page,status:id}).then(res =>{
      console.log(res);
      var data = res.data;
      if(data){
        that.setData({ couponList: res.data })
      }
      
    })
  },
  editClick(e){
    let that = this;
    console.log(e)
    var data = e.currentTarget.dataset.data;
    wx.showModal({
      title: '提示',
      content: '确认要编辑卡券吗？',
      success: function (res) {
        if (res.cancel) { } else {
          wx.navigateTo({
            url: './editCard?data=' + JSON.stringify(data),
          })
        }
      }
    })
   
    // var id = e.currentTarget.id;
  },
  //点击投放下架
  PullInSuccese:function(e){
    let that = this;
    var id = e.currentTarget.id;
    wx.showModal({
      title: '提示',
      content: '确认要投放或下架卡券吗？',
      success:function(res){
        if(res.cancel){}else{
          app.post("/coupon/put_in", "put", { id: id }).then(res => {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000,
              floorstatus: 0
            })
            that.couponList(that.data.currentTab);
          })
        }
      }
    })
    
    
  },

  NewQ:function(e){
    wx.navigateTo({
      url: '../newcard/newcard',
    })
  },
  navItem: function (e) {
    let that = this;
    var current = e.currentTarget.dataset.current;
    console.log(current);
    that.setData({
      currentTab: current,
      index: current
    })
    that.couponList(that.data.currentTab);
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.couponList(that.data.currentTab);
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.couponList(that.data.currentTab);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    that.setData({page:that.data.page+1})
    app.post("/coupon/list", "get", { page: that.data.page, status: that.data.currentTab }).then(res => {
      console.log(res);
      var data = res.data;
      if (data) {
        that.setData({ 
          couponList: that.data.couponList.concat(data) 
        })
      }

    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})